package com.java110.api.listener.fee;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Sets;
import com.java110.api.listener.AbstractServiceApiPlusListener;
import com.java110.core.annotation.Java110Listener;
import com.java110.core.context.DataFlowContext;
import com.java110.core.event.service.api.ServiceDataFlowEvent;
import com.java110.dto.RoomDto;
import com.java110.dto.fee.FeeDto;
import com.java110.dto.meterWater.WeDto;
import com.java110.dto.owner.OwnerRoomRelDto;
import com.java110.intf.community.IRoomInnerServiceSMO;
import com.java110.intf.fee.IFeeInnerServiceSMO;
import com.java110.intf.fee.IMeterWaterInnerServiceSMO;
import com.java110.intf.meter.IMeterInnerServiceSMO;
import com.java110.po.fee.PayFeePo;
import com.java110.utils.constant.lm.service.code.WeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.Set;

@Java110Listener("weTopUpConfirmListener")
public class WeTopUpConfirmListener extends AbstractServiceApiPlusListener {

    private static Logger logger = LoggerFactory.getLogger(WeTopUpConfirmListener.class);

    public static final Set<String> ELECTRICITY_VARIABLE = Sets.newHashSet("888800010016","88880001001601");
    public static final Set<String> WATER_VARIABLE = Sets.newHashSet("888800010015","88880001001501");

    @Autowired
    private IFeeInnerServiceSMO feeInnerServiceSMOImpl;
    @Autowired
    private IRoomInnerServiceSMO roomInnerServiceSMOImpl;
    @Autowired
    private IMeterInnerServiceSMO meterInnerServiceSMO;
    @Autowired
    private IMeterWaterInnerServiceSMO meterWaterInnerServiceSMOImpl;
    @Override
    protected void validate(ServiceDataFlowEvent event, JSONObject reqJson) {

    }

    @Override
    protected void doSoService(ServiceDataFlowEvent event, DataFlowContext context, JSONObject reqJson) {
        logger.debug("ServiceDataFlowEvent : {}", event);
        String oid = reqJson.getString("oId");
        FeeDto feeDto = new FeeDto();
        feeDto.setFeeId(oid);
        List<FeeDto> feeDtos = feeInnerServiceSMOImpl.queryFees(feeDto);
        if(Objects.nonNull(feeDtos) && feeDtos.size() == 1){
            FeeDto dto = feeDtos.get(0);
            // 已处理
            if(dto.getState().equals("2009001")){
                context.setResponseEntity(new ResponseEntity(HttpStatus.OK));
                return;
            }
            RoomDto room = new RoomDto();
            room.setStatusCd("0");
            room.setRoomId(dto.getPayerObjId());
            room.setCommunityId(dto.getCommunityId());
            List<RoomDto> rooms = roomInnerServiceSMOImpl.queryRooms(room);
            if(Objects.nonNull(rooms) && rooms.size() == 1){
                RoomDto dtoo = rooms.get(0);
                RoomDto params = new RoomDto();
                params.setRoomId(dto.getPayerObjId());
                params.setStatusCd("0");
                if(ELECTRICITY_VARIABLE.contains(dto.getFeeTypeCd())){
                    Double aDouble = Double.valueOf(dtoo.getElectricityNum());
                    BigDecimal add = BigDecimal.valueOf(aDouble).add(BigDecimal.valueOf(Double.valueOf(dto.getUseLevel())));
                    params.setElectricityNum(add.toString());
                    if(aDouble <= 0 && add.doubleValue() > 0){
                        // 开闸
                        WeDto dto1 = new WeDto();
                        dto1.setCommunityId(dto.getCommunityId());
                        dto1.setType("9991");
                        dto1.setRoomId(dtoo.getRoomId());
                        List<WeDto> dtos = meterWaterInnerServiceSMOImpl.queryWes(dto1);
                        if(Objects.nonNull(dtos) && dtos.size() == 1){
                            String code = dtos.get(0).getCode();
                            meterInnerServiceSMO.up(code);
                        }
                    }
                } else if(WATER_VARIABLE.contains(dto.getFeeTypeCd())){
                    BigDecimal add = BigDecimal.valueOf(Double.valueOf(dtoo.getWaterNum())).add(BigDecimal.valueOf(Double.valueOf(dto.getUseLevel())));
                    params.setWaterNum(add.toString());
                } else {
                    context.setResponseEntity(new ResponseEntity(HttpStatus.BAD_REQUEST));
                    return;
                }
                int i = roomInnerServiceSMOImpl.update(params);
                if(i != 1){
                    context.setResponseEntity(new ResponseEntity(HttpStatus.BAD_REQUEST));
                    return;
                }
                PayFeePo po = new PayFeePo();
                po.setFeeId(dto.getFeeId());
                po.setState("2009001");
                po.setStatusCd("0");
                int i1 = feeInnerServiceSMOImpl.updateFee(po);
                if(i1 != 1){
                    context.setResponseEntity(new ResponseEntity(HttpStatus.BAD_REQUEST));
                    return;
                }
                context.setResponseEntity(new ResponseEntity(HttpStatus.OK));
            } else {
                context.setResponseEntity(new ResponseEntity(HttpStatus.BAD_REQUEST));
            }
        } else {
            context.setResponseEntity(new ResponseEntity(HttpStatus.BAD_REQUEST));
        }
    }

    @Override
    public String getServiceCode() {
        return WeEnum.we_top_up_confirm.getCode();
    }

    @Override
    public HttpMethod getHttpMethod() {
        return HttpMethod.POST;
    }
}
